use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(SysDictGroup::Table)
                    .if_not_exists()
                    .comment("字典分组表")
                    .col(
                        ColumnDef::new(SysDictGroup::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(SysDictGroup::GroupName)
                            .string()
                            .char_len(32)
                            .comment("分组名称"),
                    )
                    .col(
                        ColumnDef::new(SysDictGroup::GroupCode)
                            .string()
                            .char_len(64)
                            .not_null()
                            .comment("分组编码"),
                    )
                    .col(
                        ColumnDef::new(SysDictGroup::Default)
                            .boolean()
                            .not_null()
                            .default(0)
                            .comment("是否默认,默认则不可删除"),
                    )
                    .to_owned(),
            )
            .await?;
        manager
            .create_table(
                Table::create()
                    .table(SysDict::Table)
                    .if_not_exists()
                    .comment("字典信息表")
                    .col(
                        ColumnDef::new(SysDict::Id)
                            .big_integer()
                            .not_null()
                            .auto_increment()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(SysDict::ParentId)
                            .big_integer()
                            .not_null()
                            .default(0)
                            .comment("父节点ID"),
                    )
                    .col(
                        ColumnDef::new(SysDict::Name)
                            .string()
                            .char_len(32)
                            .not_null()
                            .comment("名称"),
                    )
                    .col(
                        ColumnDef::new(SysDict::Code)
                            .string()
                            .char_len(32)
                            .comment("编码"),
                    )
                    .col(
                        ColumnDef::new(SysDict::Value)
                            .text()
                            .comment("用于存储参数"),
                    )
                    .col(
                        ColumnDef::new(SysDict::Level)
                            .integer()
                            .not_null()
                            .default(1)
                            .comment("层级"),
                    )
                    .col(
                        ColumnDef::new(SysDict::GroupId)
                            .big_integer()
                            .not_null()
                            .default(0)
                            .comment("分组ID"),
                    )
                    .col(
                        ColumnDef::new(SysDict::GroupCode)
                            .string()
                            .char_len(64)
                            .not_null()
                            .comment("分组编码"),
                    )
                    .col(
                        ColumnDef::new(SysDict::Default)
                            .boolean()
                            .not_null()
                            .default(0)
                            .comment("是否默认,默认则不可删除"),
                    )
                    .col(
                        ColumnDef::new(SysDict::DictDesc)
                            .string()
                            .char_len(100)
                            .comment("描述"),
                    )
                    .to_owned(),
            )
            .await?;
        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(SysDictGroup::Table).to_owned())
            .await?;
        manager
            .drop_table(Table::drop().table(SysDict::Table).to_owned())
            .await?;
        Ok(())
    }
}

#[derive(DeriveIden)]
enum SysDictGroup {
    Table,
    Id,
    GroupName,
    GroupCode,
    Default,
}

#[derive(DeriveIden)]
enum SysDict {
    Table,
    Id,
    Name,
    Code,
    Value,
    Level,
    ParentId,
    GroupId,
    GroupCode,
    Default,
    DictDesc,
}
